package ch.ti.bfh.app.loodel.web.utils;

import ch.ti.bfh.app.loodel.service.MemberData;
import ch.ti.bfh.app.loodel.service.create.CreateLearningEventService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.inject.Inject;


@Component("memberDataInvitationAutocompleteConverter")
public class MemberDataInvitationAutocompleteConverter extends AbstractPrimefacesAutoCompleteConverter<MemberData> {

    private final CreateLearningEventService createLearningEventService;

    @Inject
    public MemberDataInvitationAutocompleteConverter(CreateLearningEventService createLearningEventService) {
        this.createLearningEventService = createLearningEventService;
    }

    @Override
    protected MemberData getAsObjectFromAutocompleteSuggestionValue(String value) {
        if (StringUtils.isNotEmpty(value)) {
            return createLearningEventService.getMemberDataForInvitation(value);
        }
        return null;
    }

    @Override
    public String getAsStringForAutocompleteSuggestionValue(Object value) {
        if (value instanceof MemberData) {
            return ((MemberData) value).geteMail();
        }
        return null;
    }
}
